package com.example.insurance_service.service.img;


import com.example.insurance_service.dao.InsuranceDao;
import com.example.insurance_service.domain.Insurance;
import com.example.insurance_service.service.InsuranceService;
import com.example.insurance_service.service.InsuranceTypeService;
import com.example.insurance_service.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class InsuranceServiceImg implements InsuranceService {

    @Autowired
    private InsuranceDao insuranceDao;/*保险*/

    @Autowired
    private UserService userService;/*用户*/

    @Autowired
    private InsuranceTypeService insuranceTypeService;


    @Override
    public Page<Insurance> findAll(Integer pageNum, Integer pageSize, Insurance insurance) {

        Pageable of = PageRequest.of(pageNum-1, pageSize);

        Specification specification=new Specification<Insurance>(){

            @SneakyThrows
            @Override
            public Predicate toPredicate(Root<Insurance> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
               List<Predicate> list = new ArrayList<>();

               Join insureId = root.join("insureId", JoinType.LEFT);
               Join benefitId = root.join("benefitId", JoinType.LEFT);
               Join assuredId = root.join("assuredId", JoinType.LEFT);
               Join insuranceTypeId = root.join("insuranceTypeId", JoinType.LEFT);

               Path startDate = root.get("insuranceAmout");
                /*投保人*/
                String insuranceName = insurance.getInsureName();
                if (insuranceName!=null&&!insuranceName.equals("")){
                    Path userName = insureId.get("userName");
                    Predicate like = cb.like(userName, "%"+insuranceName+"%");
                    list.add(like);
                }

                /*受益人*/
                String benefitName = insurance.getBenefitName();
                if (benefitName!=null&&!benefitName.equals("")){
                    Path userName = benefitId.get("userName");
                    Predicate like = cb.like(userName, "%" + benefitName + "%");
                    list.add(like);
                }

                /*受保人*/
                String assuredName = insurance.getAssuredName();
                if (assuredName!=null&&!assuredName.equals("")){
                    Path userName = assuredId.get("userName");
                    Predicate like = cb.like(userName, "%" + assuredName + "%");
                    list.add(like);
                }

                /*保险种类*/
                String insuranceTypeName = insurance.getInsuranceTypeName();
                if (insuranceTypeName!=null && !insuranceTypeName.equals("")){
                    Path insuranceType = insuranceTypeId.get("insuranceTypename");
                    Predicate like = cb.equal(insuranceType,insuranceTypeName);
                    list.add(like);
                }

                /*时间区间查询*/
                String date1 = insurance.getDate1();
                if (date1!=null && !date1.equals("")){

                    Predicate predicate = cb.greaterThanOrEqualTo(startDate,date1);
                    list.add(predicate);
                }

                String data2 = insurance.getData2();
                if(data2!=null && !data2.equals("")){
                    Predicate predicate = cb.lessThanOrEqualTo(startDate, data2);
                    list.add(predicate);
                }


                return cb.and(list.toArray(new Predicate[list.size()]));
            }
        };

        Page page = insuranceDao.findAll(specification, of);


        return page;
    }

    @Override
    public boolean save(Insurance insurance) {
        insuranceDao.save(insurance);
        if (insurance.getId()!=null){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public void delete(Insurance insurance) {
        insuranceDao.delete(insurance);
    }
}
